//
//  AddDecryptTitleVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/22.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "AddDecryptTitleVC.h"
#import "IQTextView.h"
#import "NSString+Expand.h"

@interface AddDecryptTitleVC ()
@property (weak, nonatomic) IBOutlet IQTextView *titleTextView;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIView *demoView;

@end

@implementation AddDecryptTitleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"解密标题";
    self.titleTextView.text = self.titleStr;
    self.titleTextView.placeholder = @"请为你的解密写一个吸引人的标题，建议越具体越好。";
    self.view.backgroundColor = UIColorFromRGB(0XF2F5FA);
    [self loadRightButton];
}
-(void)loadRightButton{
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 30);
    [backBtn setTitle:@"完成" forState:UIControlStateNormal];
    backBtn.titleLabel.font = k14Font;
    [backBtn setTitleColor:UIColorFromRGB(0X474C4D) forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)finishClick{
    if (self.titleSelect) {
        self.titleSelect(self.titleTextView.text);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.markedTextRange == nil) {
        NSLog(@"text:%@", textView.text);
        if (textView.text.length > 50) {
            ALERT_VIEW(@"最多输入50个字");
            textView.text = [textView.text subString:0 length:50];
        }
        NSString *count = [NSString stringWithFormat:@"%2zd/50",textView.text.length];
        NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:count];
        //获取要调整颜色的文字位置,调整颜色
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0X42CDD9) range:NSMakeRange(0, 2)];
        
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0XA5B9C2) range:NSMakeRange(2, 3)];
        
        self.countLabel.attributedText=hintString;
        
//        self.countLabel.text = [NSString stringWithFormat:@"%2zd/50",textView.text.length];
    }
}
- (IBAction)showDemoClick:(id)sender {
    [UIView animateWithDuration:0.25 animations:^{
        self.demoView.alpha = 1;
    }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.demoView.alpha = 0;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
